package com.easy.util;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
@Aspect
public class EasyAspect {
//    @Pointcut("execution(* *com.easy.service.impl.*.*(..)),argN")
//    public void logmethod(){}

    @Around(value = "@annotation(logMethod)")
    public Object recordLog(ProceedingJoinPoint point, LogMethod logMethod) throws Throwable {
        Object obj=null;
        StringBuilder strb=new StringBuilder();
        String serviceName = logMethod.serviceName();//注解上标记的属性
        boolean decordParam = logMethod.recordParam();
        boolean decordReturn = logMethod.recordReturn();
        //获取方法签名
        MethodSignature methodSignature=(MethodSignature)point.getSignature();
        //获取方法名
        String methodName =methodSignature.getMethod().getName();//方法名称
        strb.append("[");
        strb.append(serviceName);
        strb.append("]");
        strb.append(" method:");
        strb.append(methodName);
        if(decordParam){
            strb.append(" params;");
        }
//        strb.append();
        obj = point.proceed();
        if(decordReturn){
            strb.append(" return;");
            strb.append(obj);
        }
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss SS");
        strb.append("   [");
        strb.append(sdf.format(date));
        strb.append("]");
        System.out.println(strb.toString());
        return obj;
    }
}
